﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace pyTivoMetadataManager
{
    public class StarRatingGlyph : RatingGlyph
    {
        private Color _backgroundColor = Color.Empty;
        public Color BackgroundColor
        {
            get
            {
                if (_backgroundColor.Equals(Color.Empty))
                {
                    return (Color.FromKnownColor(KnownColor.ButtonFace));
                }
                return (_backgroundColor);
            }
            set
            {
                if (_backgroundColor != value)
                {
                    _backgroundColor = value;
                }
            }
        }

        private Color _selectedColor = Color.Empty;
        public Color SelectedColor
        {
            get
            {
                if (_selectedColor.Equals(Color.Empty))
                {
                    return (Color.FromKnownColor(KnownColor.Yellow));
                }
                return (_selectedColor);
            }
            set
            {
                if (_selectedColor != value)
                {
                    _selectedColor = value;
                }
            }
        }

        private Color _hoverColor = Color.Empty;
        public Color HoverColor
        {
            get
            {
                if (_hoverColor.Equals(Color.Empty))
                {
                    return (Color.FromKnownColor(KnownColor.LightBlue));
                }
                return (_hoverColor);
            }
            set
            {
                if (_hoverColor != value)
                {
                    _hoverColor = value;
                }
            }
        }

        private Color _outlineColor;
        public Color OutlineColor
        {
            get
            {
                if (_outlineColor.Equals(Color.Empty))
                {
                    return (Color.FromKnownColor(KnownColor.ButtonShadow));
                }
                return (_outlineColor);
            }
            set
            {
                if (_outlineColor != value)
                {
                    _outlineColor = value;
                }
            }
        }

        private Brush _emptyBrush;
        protected Brush EmptyBrush
        {
            get
            {
                if (_emptyBrush == null)
                {
                    _emptyBrush = new SolidBrush(BackgroundColor);
                }
                return (_emptyBrush);
            }
            set
            {
                if (_emptyBrush != value)
                {
                    _emptyBrush = value;
                }
            }
        }

        private Brush _hoverBrush;
        protected Brush HoverBrush
        {
            get
            {
                if (_hoverBrush == null)
                {
                    _hoverBrush = new SolidBrush(BackgroundColor);
                }
                return (_hoverBrush);
            }
            set
            {
                if (_hoverBrush != value)
                {
                    _hoverBrush = value;
                }
            }
        }

        private Brush _selectedBrush;
        protected Brush SelectedBrush
        {
            get
            {
                if (_selectedBrush == null)
                {
                    _selectedBrush = new SolidBrush(BackgroundColor);
                }
                return (_selectedBrush);
            }
            set
            {
                if (_selectedBrush != value)
                {
                    _selectedBrush = value;
                }
            }
        }

        private int _outlineThickness = 1;
        public int OutlineThickness
        {
            get { return (_outlineThickness); }
            set { _outlineThickness = value; }
        }

        private Pen _outlinePen;
        protected Pen OutlinePen
        {
            get
            {
                if (_outlinePen == null)
                {
                    _outlinePen = new Pen(OutlineColor, OutlineThickness);
                }
                return (_outlinePen);
            }
            set
            {
                if (_outlinePen != value)
                {
                    _outlinePen = value;
                }
            }
        }

        /// <summary>
        /// Draws an empty unselected glyph.
        /// </summary>
        /// <param name="g">Graphics object to draw to.</param>
        /// <param name="rect">Rectangle to draw as bounds for the glyph</param>
        public override void DrawEmptyGlyph(Graphics g, Rectangle rect)
        {
            DrawGlyph(g, rect, EmptyBrush, OutlinePen);
        }

        /// <summary>
        /// Draws a glyph being hovered over.
        /// </summary>
        /// <param name="g">Graphics object to draw to.</param>
        /// <param name="rect">Rectangle to draw as bounds for the glyph</param>
        public override void DrawHoverGlyph(Graphics g, Rectangle rect)
        {
            DrawGlyph(g, rect, new LinearGradientBrush(rect, HoverColor, BackgroundColor, LinearGradientMode.ForwardDiagonal), OutlinePen);
        }

        /// <summary>
        /// Draws a selected glyph.
        /// </summary>
        /// <param name="g">Graphics object to draw to.</param>
        /// <param name="rect">Rectangle to draw as bounds for the glyph</param>
        public override void DrawSelectedGlyph(Graphics g, Rectangle rect)
        {
            DrawGlyph(g, rect, new LinearGradientBrush(rect, SelectedColor, BackgroundColor, LinearGradientMode.ForwardDiagonal), OutlinePen);
        }

        /// <summary>
        /// Draws a half selected glyph.  *Not implemented in this glyph!*
        /// </summary>
        /// <param name="g">Graphics object to draw to.</param>
        /// <param name="rect">Rectangle to draw as bounds for the glyph</param>
        public override void DrawHalfSelectedGlyph(Graphics g, Rectangle rect)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Common glyph drawing logic, draws glyph on specified graphics at rect, using supplied fill and pen styles.
        /// </summary>
        /// <param name="g"></param>
        /// <param name="rect"></param>
        /// <param name="fillBrush"></param>
        /// <param name="outlinePen"></param>
        private void DrawGlyph(Graphics g, Rectangle rect, Brush fillBrush, Pen outlinePen)
        {
            PointF[] p = new PointF[10];
            p[0].X = rect.X + (rect.Width / 2);
            p[0].Y = rect.Y;
            p[1].X = rect.X + (42 * rect.Width / 64);
            p[1].Y = rect.Y + (19 * rect.Height / 64);
            p[2].X = rect.X + rect.Width;
            p[2].Y = rect.Y + (22 * rect.Height / 64);
            p[3].X = rect.X + (48 * rect.Width / 64);
            p[3].Y = rect.Y + (38 * rect.Height / 64);
            p[4].X = rect.X + (52 * rect.Width / 64);
            p[4].Y = rect.Y + rect.Height;
            p[5].X = rect.X + (rect.Width / 2);
            p[5].Y = rect.Y + (52 * rect.Height / 64);
            p[6].X = rect.X + (12 * rect.Width / 64);
            p[6].Y = rect.Y + rect.Height;
            p[7].X = rect.X + rect.Width / 4;
            p[7].Y = rect.Y + (38 * rect.Height / 64);
            p[8].X = rect.X;
            p[8].Y = rect.Y + (22 * rect.Height / 64);
            p[9].X = rect.X + (22 * rect.Width / 64);
            p[9].Y = rect.Y + (19 * rect.Height / 64);

            g.FillPolygon(fillBrush, p);
            g.DrawPolygon(outlinePen, p);
        }
    }
}
